home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry2.iso / Cube Drop 2001 1.0 / src / Matrix.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  7.1 KB  |  305 lines

  1. #include "matrix.h"
  2.  
  3. Matrix::Matrix()
  4. {
  5.     score = 0;
  6.     ZeroMatrix();
  7. }
  8.  
  9. void Matrix::ZeroMatrix()
  10. {
  11.     for(int i = 0; i < MAT_WIDTH; ++i)
  12.         for(int j = 0; j < MAT_HEIGHT; ++j)
  13.             for(int k = 0; k < MAT_DEPTH; ++k)
  14.                 loc[i][j][k] = 0;
  15. }
  16.  
  17. int Matrix::isSet(int x, int y, int z)
  18. {
  19.     //return the value(color) stored in the matrix at x,y,z
  20.     //if the value is 0, it is not set and therefore
  21.     //no cube is located there
  22.     return(loc[x][y][z]);    
  23. }
  24.  
  25. void Matrix::set(int x, int y, int z)
  26. {
  27.     int color;
  28.     
  29. //    srand( (unsigned)time(NULL) );//seed the rand num generator
  30.  
  31.     color = rand()%6+1; //0=no cube, 1 = red, 2=green, 3=blue,
  32.                         //4=yellow, 5=cyan, 6=magenta
  33.  
  34.     setActiveCube(x,y,z);
  35.  
  36.     //arrays are zero based so subtract one
  37.     --x;
  38.     --y;
  39.     --z;
  40.  
  41.     loc[x][y][z] = color;
  42. }
  43.  
  44. void Matrix::moveLeft()
  45. {
  46.     int temp;
  47.  
  48.     //cube can only be moved if it is not at rest
  49.     if( (activeCube.x > 0) && (activeCube.y != 0) &&
  50.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  51.     {
  52.         //move cube left if the space is not already occupied
  53.         if(loc[activeCube.x-1][activeCube.y][activeCube.z]== 0)
  54.         {
  55.             //store the color value of the cube and move it
  56.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  57.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  58.             activeCube.x = activeCube.x - 1;
  59.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  60.         }
  61.     }
  62. }
  63.  
  64. void Matrix::moveRight()
  65. {
  66.     int temp;
  67.  
  68.     //cube can only be moved if it is not at rest
  69.     if( (activeCube.x < 4) && (activeCube.y != 0) &&
  70.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  71.     {
  72.         //store the color value of the cube and move it
  73.         if(loc[activeCube.x+1][activeCube.y][activeCube.z]== 0)
  74.         {
  75.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  76.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  77.             activeCube.x = activeCube.x + 1;
  78.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  79.         }
  80.     }
  81. }
  82.  
  83. void Matrix::moveIn()
  84. {
  85.     int temp;
  86.  
  87.     //cube can only be moved if it is not at rest
  88.     if( (activeCube.z < 4) && (activeCube.y != 0) &&
  89.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  90.     {
  91.         //store the color value of the cube and move it
  92.         if(loc[activeCube.x][activeCube.y][activeCube.z+1]== 0)
  93.         {
  94.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  95.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  96.             activeCube.z = activeCube.z + 1;
  97.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  98.         }
  99.     }
  100. }
  101.  
  102. void Matrix::moveOut()
  103. {
  104.     int temp;
  105.  
  106.     //cube can only be moved if it is not at rest
  107.     if( (activeCube.z > 0) && (activeCube.y != 0) &&
  108.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  109.     {
  110.         //store the color value of the cube and move it
  111.         if(loc[activeCube.x][activeCube.y][activeCube.z-1]== 0)
  112.         {
  113.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  114.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  115.             activeCube.z = activeCube.z - 1;
  116.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  117.         }
  118.     }
  119. }
  120.  
  121. void Matrix::lowerFloaters(char rowDir)
  122. {
  123.     const MatrixLocation tempLoc = activeCube;
  124.     int startHeight = 1 + activeCube.y;
  125.  
  126.     //go through evey location above the row that was cleared
  127.     //and drop all of the cubes
  128.     for(int j = 0; j < MAT_WIDTH; ++j)
  129.     {
  130.         for(int i = startHeight; i < MAT_HEIGHT; ++i)
  131.         {
  132.             if(rowDir == 'x')
  133.             {
  134.                 //activeCube.x = activeCube.x;
  135.                 activeCube.y = i;
  136.                 activeCube.z = j;
  137.  
  138.             }
  139.             else // rowDir == 'z'
  140.             {
  141.                 activeCube.x = j;
  142.                 activeCube.y = i;
  143.                 //activeCube.z = activeCube.z;
  144.             }
  145.  
  146.             //if there is a cube in the spot, drop it
  147.             if(loc[activeCube.x][activeCube.y][activeCube.z])
  148.                 drop();
  149.         }
  150.     }
  151.  
  152.     activeCube = tempLoc;
  153. }
  154.  
  155.  
  156. void Matrix::SetTitleBar()
  157. {
  158.     char scorePhrase[50];
  159.  
  160.     sprintf(scorePhrase,
  161.         "Cube Space                     Score: %i", score);
  162.     glutSetWindowTitle(scorePhrase);
  163. }
  164.  
  165.  
  166. void Matrix::clearRows()
  167. {
  168.     int oldScore = score;
  169.     int color;
  170.  
  171.     for(int y=0; y<MAT_HEIGHT; ++y)
  172.     {
  173.         for(int x=0; x<MAT_WIDTH; ++x)
  174.         {
  175.             //for a given value of x, if all cells along z are set
  176.             // to the same color, then clear the row
  177. //            if(loc[x][y][0] && loc[x][y][1] && loc[x][y][2] && loc[x][y][3] && loc[x][y][4])
  178.             color = loc[activeCube.x][activeCube.y][activeCube.z];
  179.             if( color == loc[x][y][0] &&
  180.                 color == loc[x][y][1] &&
  181.                 color == loc[x][y][2] &&
  182.                 color == loc[x][y][3] &&
  183.                 color == loc[x][y][4]
  184.               )
  185.             {
  186.                 //increase the score by the values in the row
  187.                 score += loc[x][y][0];
  188.                 score += loc[x][y][1];
  189.                 score += loc[x][y][2];
  190.                 score += loc[x][y][3];
  191.                 score += loc[x][y][4];
  192.  
  193.                 loc[x][y][0] = 0;
  194.                 loc[x][y][1] = 0;
  195.                 loc[x][y][2] = 0;
  196.                 loc[x][y][3] = 0;
  197.                 loc[x][y][4] = 0;
  198.  
  199.                 lowerFloaters('x');
  200.             }
  201.         }
  202.  
  203.         for(int z=0; z<MAT_WIDTH; ++z)
  204.         {
  205.             //for a given value of z, if all cells along x are set
  206.             //to the same color, then clear the row
  207. //            if(loc[0][y][z] && loc[1][y][z] && loc[2][y][z] && loc[3][y][z] && loc[4][y][z])
  208.             color = loc[activeCube.x][activeCube.y][activeCube.z];
  209.             if( color == loc[0][y][z] &&
  210.                 color == loc[1][y][z] &&
  211.                 color == loc[2][y][z] &&
  212.                 color == loc[3][y][z] &&
  213.                 color == loc[4][y][z]
  214.               )
  215.             {
  216.                 //increase the score by the values in the row
  217.                 score += loc[0][y][z];
  218.                 score += loc[1][y][z];
  219.                 score += loc[2][y][z];
  220.                 score += loc[3][y][z];
  221.                 score += loc[4][y][z];
  222.  
  223.                 loc[0][y][z] = 0;
  224.                 loc[1][y][z] = 0;
  225.                 loc[2][y][z] = 0;
  226.                 loc[3][y][z] = 0;
  227.                 loc[4][y][z] = 0;
  228.  
  229.                 lowerFloaters('z');
  230.             }
  231.         }
  232.     } //end of y loop
  233.  
  234.     if(oldScore != score)
  235.         SetTitleBar();
  236. }
  237.  
  238. bool Matrix::moveDown()
  239. {
  240.     int temp = 0;
  241.     int lowestOpenSpot = 0;
  242.  
  243.     //find the lowest empty spot in the current column
  244.     while(loc[activeCube.x][lowestOpenSpot][activeCube.z])
  245.     {
  246.         ++lowestOpenSpot;
  247.         if(lowestOpenSpot >= MAT_HEIGHT)
  248.             return false; //game over        
  249.     }
  250.  
  251.     //if the active cube is not immediately above the lowest
  252.     //spot, then lower the cube by one cell
  253.     if(activeCube.y > lowestOpenSpot)
  254.     {
  255.         temp = loc[activeCube.x][activeCube.y][activeCube.z];
  256.         loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  257.         activeCube.y = activeCube.y - 1;
  258.         loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  259.     }
  260.     else //start a new cube
  261.     {
  262.         //clear any completed rows
  263.         clearRows();
  264.         set(3,10,3);
  265.     }
  266.  
  267.     return true;
  268. }
  269.  
  270. bool Matrix::drop()
  271. {
  272.     int temp = 0;
  273.     int lowestOpenSpot = 0;
  274.  
  275.     while(loc[activeCube.x][lowestOpenSpot][activeCube.z])
  276.     {
  277.         ++lowestOpenSpot;
  278.         if(lowestOpenSpot >= MAT_HEIGHT)
  279.             return false; //game over
  280.     }
  281.  
  282.     //if the cube is not at the lowest open spot, drop it to the lowest spot
  283.     if(activeCube.y > lowestOpenSpot)
  284.     {
  285.         temp = loc[activeCube.x][activeCube.y][activeCube.z]; //save the color value
  286.         loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  287.         activeCube.y = lowestOpenSpot;
  288.         loc[activeCube.x][activeCube.y][activeCube.z] = temp; //restore the color
  289.     }
  290. //    else
  291. //    {
  292. //        //check for completed rows, then start a new cube
  293. //        clearRows();
  294. //        set(3,10,3);
  295. //    }
  296.     return true;
  297. }
  298.  
  299. void Matrix::setActiveCube(int xx, int yy, int zz)
  300. {
  301.     activeCube.x = (--xx);
  302.     activeCube.y = (--yy);
  303.     activeCube.z = (--zz);
  304. }
  305.